#!/usr/bin/php
<?php
/**	
*	builder.php is used to satisfy all of the dependencies of AndroBuild.php for use on the command line.  Allows for the building
*	of the andromeda application through the command line, instead of on the web installer.
*/
if ( !isset( $ARGV['1'] ) ) {
    echo( 'Error please supply the name of the application' ."\r\n" );
    die(1);
} else {
    $app_name = $ARGV['1'];
}

$include_path = realpath('../');
$base = realpath('../../');
ini_set('include_path',ini_get('include_path').':' .$include_path);

/*
* Necessary includes (Dependencies for builder)
*/
$force_cli = true;

#include("/var/www/andro_root/andro/index.php");
#include("/var/www/andro_root/andro/lib/androLib.php");
include("lib/spyc/spyc.php");


/*
* Hardcode depended $GLOBALS
*/
$GLOBALS["parm"] = array(
            "DBSERVER_URL"=>"localhost"
            ,"UID"=>"postgres"
            ,"DIR_PUBLIC"=>$base
            ,"DIR_PUBLIC_APP"=>$app_name
            ,"APP"=>$app_name
            ,"APPDSC"=>"Andromeda Node Manager" //Not sure why this is needed
            ,"SPEC_BOOT"=>"AndroDBB"
            ,"SPEC_LIB"=>"andro_universal"
            ,"SPEC_LIST"=>$app_name .'dd.yaml'
         );
$GLOBALS["x_password"] = '';
$GLOBALS["AG"]["dirs"]["dyanmic"] = $base .'/dynamic/';
$GLOBALS["AG"]["dirs"]["root"] = $base .'/andro/';
$GLOBALS["AG"]["application"] = $app_name;

/*
* Connect to database
*/
connect();

/*
* Build application
*/
include('androInstallBuild.php');

/*
* Insert new webpaths record
* @todo what is this used for??
*/
$dir_pub= realpath(dirname(__FILE__).'/../..');
if(strpos(ArraySafe($_ENV,'OS',''),'indows')!==false) {
	$dir_pub = str_replace("\\","\\\\",$dir_pub);
}
$row=array(
	'webpath'=>'DEFAULT'
	,'dir_pub'=>$dir_pub
	,'description'=>'Default Web Path'
	);
SQLX_Insert('webpaths',$row);



/*
* Insert new applications record
* @todo We need to make this only occur if app doesnt exist
*/
$row=array(
  'application'=>$app_name
  ,'description'=>"Andromeda Node Manager" // Shouldnt matter
  ,'appspec'=>'andro.dd.yaml'
  ,'node'=>'LOCAL'
  ,'webpath'=>'DEFAULT'
  ,'flag_svn'=>'Y'
  ,'svn_url'=>'http://andro.svn.sourceforge.net/svnroot/andro/releases/' // needs to be change
);
SQLX_Insert('applications',$row);

/*
* End - Close connection
*/
disconnect();



//==================================================================
//	FUNCTIONS
//==================================================================

/**
* Connect to the postgresql database server with the default installed username
*/
function connect() {
	$cnx = 
		' dbname=' .$app_name .
		' user=start'.
		' password=start';
	$GLOBALS["dbconn"] = pg_connect($cnx,PGSQL_CONNECT_FORCE_NEW);
}

/**
* Disconnect from the postgresql database server
*/
function disconnect() {
	pg_close($GLOBALS["dbconn"]);
}

?>
